﻿using CDSysLDManageProject.BYDTableAdapters;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace CDSysLDManageProject
{
    public partial class FormSECProcessSet : Form
    {
        rt_ProcessInfoTableAdapter processinfo = new rt_ProcessInfoTableAdapter();
        //tbl_processtypeinfoTableAdapter pinfo = new tbl_processtypeinfoTableAdapter();
        ServTimeGetTableAdapter sertime = new ServTimeGetTableAdapter();
        DataTable dtprocess = new DataTable();
        DataTable dtprocesstype = new DataTable();

        public FormSECProcessSet()
        {
            InitializeComponent();
            System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false;
            dgvprocess.AutoGenerateColumns = false;
            dtprocess.Columns.Add("processtype");
            dtprocess.Columns.Add("secprocessno");
            dtprocess.Columns.Add("kpprocessno");

            //processtype.Items.Add("返工");
            //processtype.Items.Add("300AH");
            //processtype.Items.Add("正常C15");
            //processtype.Items.Add("正常C19");
            dgvprocess.AllowUserToAddRows = false;
            //dtprocess = processinfo.GetProcessInfo();
           
            //dgvprocess.DataSource = dtprocess;
            //dtprocess = processinfo.GetProcessInfo();
            ////dtprocess = dtprocess2;
            //dgvprocess.DataSource = dtprocess;
            //dtprocesstype = pinfo.GetProcessTypeInfo();
            //if (dtprocesstype.Rows.Count > 0)
            //{
            //    for (int i = 0; i < dtprocesstype.Rows.Count; i++)
            //    {
            //        string strprocesstype = dtprocesstype.Rows[i]["processtype"].ToString();
            //        processtype.Items.Add(strprocesstype);
            //    }

            //}
            //dtprocess = processinfo.GetProcessInfo();
            //dgvprocess.DataSource = dtprocess;

        }
        
       
        private void FormSetCDArg_Load(object sender, EventArgs e)
        {
            dtprocess = processinfo.GetProcessInfo();
            DataRow[] dr = dtprocess.Select();
            if (dr.Length > 0)
            {
                for (int i = 0; i < dr.Length; i++)
                {
                    dr[i]["secprocessno"] = int.Parse(dr[i]["secprocessno"].ToString()) + 1;
                }
            }
            dgvprocess.DataSource = dtprocess;
            
        }

        private void button2_Click(object sender, EventArgs e)
        {

            StaticArg.CheckStatus = "open";
            FormClosedCheck cc = new FormClosedCheck();
            cc.ShowDialog();
            //验证通过了才可以修改
            if (StaticArg.CheckStatus == "close")
            {
                for (int i = 0; i < dgvprocess.RowCount; i++)
                {

                    if (dgvprocess.Rows[i].Cells["processtype"].Value == null && dgvprocess.Rows[i].Cells["processno"].Value == null)
                    {
                        MessageBox.Show("请输入完整的工艺信息!");
                        continue;
                    }
                    if (dgvprocess.Rows[i].Cells["processtype"].Value.ToString() == "" || dgvprocess.Rows[i].Cells["secprocessno"].Value.ToString() == "" || dgvprocess.Rows[i].Cells["kpprocessno"].Value.ToString() == "")
                    {
                        MessageBox.Show("请输入完整的工艺信息!");
                        return;
                    }
                    

                    //不存在KID则insert
                    if (dgvprocess.Rows[i].Cells["kid"].Value.ToString() == "0")
                    {
                        DateTime dt = DateTime.Parse(sertime.ScalarQuery().ToString());
                        try
                        {
                            processinfo.InsertProcessType(dgvprocess.Rows[i].Cells["processtype"].Value.ToString(), dgvprocess.Rows[i].Cells["kpprocessno"].Value.ToString(), int.Parse(dgvprocess.Rows[i].Cells["secprocessno"].Value.ToString())-1, DateTime.Parse(sertime.ScalarQuery().ToString()), null, null, null);
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show("将工艺信息存到数据失败,请输入正确的参数!");
                            Log.SystemError.Add("将工艺信息存到数据失败,请输入正确的参数!" + ex.ToString());
                        }

                    }//否则看是否修改
                    else
                    {
                        int ProcessCount = int.Parse(processinfo.GetProcessCountByTypeAndNo(dgvprocess.Rows[i].Cells["processtype"].Value.ToString(), int.Parse(dgvprocess.Rows[i].Cells["secprocessno"].Value.ToString())-1, dgvprocess.Rows[i].Cells["kpprocessno"].Value.ToString()).ToString());
                        if (ProcessCount > 0)
                        {

                        }
                        else
                        {

                            DateTime dt = DateTime.Parse(sertime.ScalarQuery().ToString());
                            try
                            {
                                processinfo.UpdateProcessByKid(dgvprocess.Rows[i].Cells["processtype"].Value.ToString(), dgvprocess.Rows[i].Cells["kpprocessno"].Value.ToString(), int.Parse(dgvprocess.Rows[i].Cells["secprocessno"].Value.ToString())-1, DateTime.Parse(sertime.ScalarQuery().ToString()), int.Parse(dgvprocess.Rows[i].Cells["kid"].Value.ToString()));
                            }
                            catch (Exception ex)
                            {
                                MessageBox.Show("将工艺信息同步到数据失败,请输入正确的参数!");
                                Log.SystemError.Add("将工艺信息同步到数据失败,请输入正确的参数!" + ex.ToString());
                            }


                        }
                    }

                }
                FormSetCDArg_Load(null, null);
            }
            else
            { 
            
            }
            
           
            
        }

        private void btdelete_Click(object sender, EventArgs e)
        {

           
            StaticArg.CheckStatus = "open";
            FormClosedCheck cc = new FormClosedCheck();
            cc.ShowDialog();

            if (StaticArg.CheckStatus == "close")
            {

                if (dgvprocess.SelectedRows.Count >= 1)
                {
                    for (int i = 0; i < dgvprocess.SelectedRows.Count; i++)
                    {
                        if (dgvprocess.SelectedRows[i].Cells["processtype"].Value.ToString() == "正常C15" || dgvprocess.SelectedRows[i].Cells["processtype"].Value.ToString() == "正常C19")
                        {
                            MessageBox.Show("不可以删除正常工艺！");
                            return;
                        }

                        int kid = int.Parse(dgvprocess.SelectedRows[i].Cells["kid"].Value.ToString());
                        try
                        {
                            processinfo.DeleteByKid(kid);
                        }
                        catch (Exception ex)
                        {

                            Log.SystemError.Add("删除工艺信息失败，原因是:" + ex.ToString());
                            MessageBox.Show("删除工艺信息失败!");
                        }
                    }

                    //dgvprocess.DataSource = dtprocess;
                    FormSetCDArg_Load(null, null);
                }
            }
            else
            { 
            }
            
        }

        private void button1_Click(object sender, EventArgs e)
        {
           
            try
            {
                dtprocess.Rows.Add(0);
            }
            catch (Exception)
            {

                MessageBox.Show("请先完成上一行的编辑!");
            }
          
        }

       
  
       
    }
}
